package com.sgb.kjwl.view.ui.fragment.self;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtPackersManageIntroduceBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfPackersManageActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamIntroduceActivity;
import com.sgb.link.view.widget.ExpandedTextView;
import com.sgb.kjwl.viewmodel.self.packers.FragIntroduceModel;
import com.swgk.core.base.di.AppComponent;

import javax.inject.Inject;

import static android.app.Activity.RESULT_OK;

public class FragIntroduceFragment extends AppLazyFragment {

    @Inject
    FragIntroduceModel viewModel;
    private AtPackersManageIntroduceBinding binding;

    private SelfTeamIntroduceEntity entity;

    public static final int REQUEST_EDIT_CODE = 10032;

    public static FragIntroduceFragment newInstance() {
        FragIntroduceFragment fragment = new FragIntroduceFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.at_packers_manage_introduce, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        binding.teamPublish.setOnClickListener(this);
        binding.tvEdit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.team_publish:
              Intent intent = new Intent(getActivity(), SelfTeamIntroduceActivity.class);
              startActivityForResult(intent, REQUEST_EDIT_CODE);
              break;
          case R.id.tv_edit:
              Intent intent1 = new Intent(getActivity(), SelfTeamIntroduceActivity.class);
              intent1.putExtra(AppConstant.EXTRA_ENTITY, entity);
              startActivityForResult(intent1, REQUEST_EDIT_CODE);
              break;
      }
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getTeamIntroduce();
    }

    public void updateIntroduce(SelfTeamIntroduceEntity entity) {
        if (entity == null) {
            binding.layoutNullTeam.setVisibility(View.VISIBLE);
        } else {
            binding.layoutNullTeam.setVisibility(View.GONE);
            this.entity = entity;
            binding.setViewModel(entity);
            switch (entity.getTeamType()) {
                case 0:
                    binding.tvTeamType.setText("施工队");
                    break;
                case 1:
                    binding.tvTeamType.setText("班组工队");
                    break;
                case 2:
                    binding.tvTeamType.setText("承包商");
                    break;
            }
            binding.tvTeamSize.setText(entity.getTeamSize() + "人");
            String categoryName = entity.getCategoryName();
            if (!TextUtils.isEmpty(categoryName)) {
                binding.tiBusinessText.setText(categoryName.replaceAll(",", "、"));
            } else {
                binding.tiBusinessText.setText("");
            }
            String address = entity.getServiceAreaName();
            if (!TextUtils.isEmpty(address)) {
                StringBuilder builder = new StringBuilder();
                String[] split = address.split("、");
                for (int i = 0; i < split.length; i++) {
                    builder.append(split[i] + "/");
                }
                binding.tvServiceArea.setVisibility(View.VISIBLE);
                binding.tvServiceArea.setText(builder.substring(0, builder.length() - 1));
            }
            new ExpandedTextView().showText(binding.tvBasic, binding.tvBasicExpand);
            new ExpandedTextView().showText(binding.tvHistory, binding.tvHistoryExpand);
            new ExpandedTextView().showText(binding.tvStructure, binding.tvStructureExpand);
            new ExpandedTextView().showText(binding.tvIdea, binding.tvIdeaExpand);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_EDIT_CODE) {
                viewModel.getTeamIntroduce();
                ((SelfPackersManageActivity) getActivity()).refreshData();
            }
        }
    }

    public void refreshTeamData(){
        viewModel.getTeamIntroduce();
    }
}
